# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:

from PIL import Image
# pic = Image.open('./20-203A1.gif')
# # jpg.save('./20-203A1.png')
# print(pic.size)
# print(pic.format)

# def modify_picture_format(src_path, dest_path, dest_name, format):
#     img = Image.open(src_path)
#     if dest_path[-1:] != '/':
#         dest_path = dest_path + '/'
#     format = format if format[:1] == '.' else '.'+format
#     dest_path = dest_path+dest_name+format
#     if img.format == 'PNG':
#         return {'code': 1, 'info': 'sucess', 'width': img.size[0], 'height': img.size[1]}
#     try:
#         img.save(dest_path)
#
#         return {'code': 1, 'info': 'sucess', 'width': img.size[0], 'height': img.size[1]}
#     except Exception as e:
#         return {'code': 0, 'info': e.__repr__(), 'width': img.size[0], 'height': img.size[1]}

# print(modify_picture_format('./20-203A1.gif', './', '20-203A1', 'png'))



# 图片抓下来后需要先格式化图片, 然后在进行描点清洗
def resize_img(filein, fileout, width, height, type):
    img = Image.open(filein)
    out = img.resize((width, height), Image.ANTIALIAS)
    out.save(fileout, type)

path = '/Users/duanmengdi/Desktop/workspace/py3_code/hk/hyundai_img/CCHNPSQ18/'
dest_path = '/Users/duanmengdi/Desktop/workspace/py3_code/hk/hyundai_img/CCHNPSQ18_format/'
import os
img_list = os.listdir(path)
print(len(img_list))
for img in img_list:
    if 'DS' in img:
        continue
    img_path = path+img
    Img = Image.open(img_path)
    width = Img.size[0]
    height = Img.size[1]
    size = (width, height)
    print(img, size)
    if width > height:
        resize_img(path+img, dest_path+img.replace('.gif', '.png').replace("-", ""), 1200, 820, 'png')
    else:
        resize_img(path+img, dest_path+img.replace('.gif', '.png').replace("-", ""), 600, 820, 'png')
    # dest_pth = img_path.replace('-', '')
    # print(img_path, dest_pth)
    # comd = 'mv "{}" "{}"'.format(img_path, dest_pth)
    # os.system(comd)




